package com.adtech.farmProducts.utils;

import com.adtech.farmProducts.model.DataBean;

import org.greenrobot.eventbus.EventBus;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Time:2020/5/6
 * Created by xiaocz
 * Describe
 */
public class CloudOrderDownTimer {

    private TimerTask task;
    private int time;

    public CloudOrderDownTimer(int millisInFuture) {
        time = millisInFuture;
    }

    public void onStart() {
        task = new TimerTask() {
            int timeRemaining = 0;
            @Override
            public void run() {
                timeRemaining++;
                DataBean dataBean=new DataBean();
                if (timeRemaining == time) {
                    dataBean.setMoney(0);
                    EventBus.getDefault().postSticky(dataBean);
                    Constants.cloudIsShow = false;
                    task.cancel();
                } else {
                    dataBean.setMoney(timeRemaining);
                    dataBean.setCount(time - timeRemaining);
                    EventBus.getDefault().postSticky(dataBean);
                }
            }
        };
        Timer timer = new Timer();
        // 1000，延时1秒后执行。
        // 2000，每隔2秒执行1次task。
        timer.schedule(task, 1000, 1000);
    }

    public void onStop(){
        if(task!=null){
            task.cancel();
            task=null;
        }
    }
}